/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package radio_station7.model;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author ebababi
 */
@Entity
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Band.findAll", query = "SELECT b FROM Band b"),
    @NamedQuery(name = "Band.findById", query = "SELECT b FROM Band b WHERE b.id = :id"),
    @NamedQuery(name = "Band.findByName", query = "SELECT b FROM Band b WHERE b.name = :name"),
    @NamedQuery(name = "Band.findByFormationDate", query = "SELECT b FROM Band b WHERE b.formationDate = :formationDate")})
public class Band implements Serializable {
    @Transient
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(nullable = false)
    private Long id;
    @Basic(optional = false)
    @Column(nullable = false, length = 255)
    private String name;
    @Basic(optional = false)
    @Column(name = "FORMATION_DATE", nullable = false)
    @Temporal(TemporalType.DATE)
    private Date formationDate;
    @JoinTable(name = "BAND_ARTIST", joinColumns = {
        @JoinColumn(name = "BAND_ID", referencedColumnName = "ID", nullable = false)}, inverseJoinColumns = {
        @JoinColumn(name = "ARTIST_ID", referencedColumnName = "ID", nullable = false)})
    @ManyToMany
    private Collection<Artist> artistCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "bandId")
    private Collection<BandAlbum> albumCollection;

    public Band() {
    }

    public Band(Long id) {
        this.id = id;
    }

    public Band(Long id, String name, Date formationDate) {
        this.id = id;
        this.name = name;
        this.formationDate = formationDate;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        Long oldId = this.id;
        this.id = id;
        changeSupport.firePropertyChange("id", oldId, id);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        String oldName = this.name;
        this.name = name;
        changeSupport.firePropertyChange("name", oldName, name);
    }

    public Date getFormationDate() {
        return formationDate;
    }

    public void setFormationDate(Date formationDate) {
        Date oldFormationDate = this.formationDate;
        this.formationDate = formationDate;
        changeSupport.firePropertyChange("formationDate", oldFormationDate, formationDate);
    }

    @XmlTransient
    public Collection<Artist> getArtistCollection() {
        if (artistCollection == null)
            this.artistCollection = new ArrayList<Artist>();

        return artistCollection;
    }

    public void setArtistCollection(Collection<Artist> artistCollection) {
        Collection<Artist> oldArtistCollection = this.artistCollection;
        this.artistCollection = artistCollection;
        changeSupport.firePropertyChange("artistCollection", oldArtistCollection, artistCollection);
    }

    public void addArtistCollection(Artist artist) {
        Collection<Artist> oldArtistCollection = this.artistCollection;
        // TODO: Clean up dirty fix for change support to work.
        Collection<Artist> list = new ArrayList<Artist>();
        for (Artist a : this.artistCollection) {
            list.add(a);
        }
        list.add(artist);
        this.artistCollection = list;
        changeSupport.firePropertyChange("artistCollection", oldArtistCollection, artistCollection);
    }

    public void removeArtistCollection(Artist artist) {
        Collection<Artist> oldArtistCollection = this.artistCollection;
        // TODO: Clean up dirty fix for change support to work.
        Collection<Artist> list = new ArrayList<Artist>();
        for (Artist a : this.artistCollection) {
            list.add(a);
        }
        list.remove(artist);
        this.artistCollection = list;
        changeSupport.firePropertyChange("artistCollection", oldArtistCollection, artistCollection);
    }

    @XmlTransient
    public Collection<BandAlbum> getAlbumCollection() {
        return albumCollection;
    }

    public void setAlbumCollection(Collection<BandAlbum> albumCollection) {
        this.albumCollection = albumCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Band)) {
            return false;
        }
        Band other = (Band) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return String.format("%s", name);
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.removePropertyChangeListener(listener);
    }
    
}
